/*
 * events_dispatcher.cpp
 *
 *  Created on: 04.05.2011
 *      Author: inkooboo
 */

#include "logger.hpp"
#include "core/event.hpp"
#include "f_engine.hpp"

#include "events_dispatcher.hpp"

#include <boost/bind.hpp>
#include <sstream>

boost::shared_ptr<if_system_t> events_dispatcher_t::create(f_engine_t *engine)
{
    boost::shared_ptr<events_dispatcher_t> ret(new events_dispatcher_t(engine));
    logger::log(DEBUG, "[Events dispatcher] Events dispatcher system created.");
    return ret;
}

void events_dispatcher_t::start()
{
    server_.listen("events_channel");
    server_.set_data_handler(boost::bind(&events_dispatcher_t::data_handler, this, _1, _2));

    engine_->report_started();
}

void events_dispatcher_t::data_handler(const char *data, size_t size)
{
    std::stringstream stream;
    stream.write(data, size);
    event_t event(stream);

    logger::log(DEBUG) << "Dispatching event: " << event.serialize();
}
